package juc;

import org.junit.Test;
import util.ThreadUtil;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author wuguojian
 * @description
 * @date 2022/11/28 下午4:20
 **/
public class ConditionTest {

    final ReentrantLock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();

    public void awaitTest() throws Exception{
        lock.lock();
        try {
            System.out.println("await");
            condition.await();
            System.out.println("解除等待");
        } finally {
            lock.unlock();
        }
    }

    public void signalTest(){
        lock.lock();
        try {
            System.out.println("signal");
            condition.signal();
            System.out.println("继续执行");
        } finally {
            lock.unlock();
        }

    }

    @Test
    public void test() {
        new Thread(()->{
            try {
                awaitTest();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()->{
            signalTest();
        }).start();

        ThreadUtil.sleep(200L);
    }

}
